<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="1050px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" :rules="formRules" :inline="true">
                <!-- <el-form-item label="客户编号" prop="customerId">
          <el-input
            v-model="formData.customerId"
            placeholder="请输入客户编号"
          />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input
            v-model="formData.customerName"
            placeholder="请输入客户名称"
          />
        </el-form-item>
        <el-form-item label="客户联系人编号" prop="customerContactId">
          <el-input
            v-model="formData.customerContactId"
            placeholder="请输入客户联系人编号"
          />
        </el-form-item>-->
                <el-form-item label="客户联系人" prop="customerContact">
                    <el-input class="w-[320px]" v-model="formData.customerContact" placeholder="请输入客户联系人" />
                </el-form-item>

                <el-form-item label="联系时间" prop="contactTime">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.contactTime"
                        style="width: 320px"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择联系时间"
                    />
                </el-form-item>
                <el-form-item label="下次跟进时间" prop="followUpTime">
                    <el-date-picker
                        class="flex-1 !flex"
                        v-model="formData.followUpTime"
                        style="width: 320px"
                        type="datetime"
                        clearable
                        value-format="YYYY-MM-DD hh:mm:ss"
                        placeholder="请选择下次跟进时间"
                    />
                </el-form-item>
                <el-form-item label="联系方式" prop="contactWay">
                    <el-input class="w-[320px]" v-model="formData.contactWay" placeholder="请输入联系方式" />
                </el-form-item>
                <el-form-item label="联系类型" prop="contactType">
                    <el-select
                        class="flex-1 w-[320px]"
                        v-model="formData.contactType"
                        placeholder="请选择联系类型"
                    >
                        <el-option
                            v-for="(item, index) in dictData.test"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                            clearable
                            :disabled="!item.status"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="上传附件" prop="fileName">
                    <el-input class="w-[320px]" v-model="formData.fileName" placeholder="请输入上传文件名称" />
                </el-form-item>
                <el-form-item label="联系内容" prop="contactContent">
                    <!-- <editor v-model="formData.contactContent" :height="200" /> -->
                    <el-input
                        type="textarea" class="w-[320px]"
                        v-model="formData.contactContent"
                        placeholder="请输入备注"
                    />
                </el-form-item>
                <el-form-item label="我方联系人" prop="ourContactName">
                    <el-input class="w-[320px]" v-model="formData.ourContactName" placeholder="请输入我方联系人" />
                </el-form-item>
                <!-- <el-form-item label="上传文件名称" prop="fileName">
          <el-input
            v-model="formData.fileName"
            placeholder="请输入上传文件名称"
          />
        </el-form-item>
        <el-form-item label="上传文件路径" prop="filePath">
          <el-input
            v-model="formData.filePath"
            placeholder="请输入上传文件路径"
          />
        </el-form-item> -->
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import { clientPhoneEdit, clientPhoneAdd, clientPhoneDetail } from '@/api/clientPhone'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '修改联系记录' : '新增联系记录'
})

const formData = reactive({
    id: '',
    customerName: '',
    customerContactId: '',
    customerContact: '',
    contactContent: '',
    contactTime: '',
    ourContactId: '',
    ourContactName: '',
    contactWay: '',
    contactTypeId: '',
    contactType: '',
    creatorId: '',
    creatorName: '',
    followUpTime: '',
    fileName: '',
    filePath: ''
})

const formRules = {
    //   customerId: [
    //     {
    //       required: true,
    //       message: "请输入客户编号",
    //       trigger: ["blur"],
    //     },
    //   ],
    //   customerName: [
    //     {
    //       required: true,
    //       message: "请输入客户名称",
    //       trigger: ["blur"],
    //     },
    //   ],
    customerContact: [
        {
            required: true,
            message: '请输入客户联系人',
            trigger: ['blur']
        }
    ],
    contactContent: [
        {
            required: true,
            message: '请输入联系内容',
            trigger: ['blur']
        }
    ],
    contactTime: [
        {
            required: true,
            message: '请选择联系时间',
            trigger: ['blur']
        }
    ],
    ourContactName: [
        {
            required: true,
            message: '请输入我方联系人',
            trigger: ['blur']
        }
    ],
    contactWay: [
        {
            required: true,
            message: '请输入联系方式',
            trigger: ['blur']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await clientPhoneEdit(data) : await clientPhoneAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await clientPhoneDetail({
        id: row.id
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
